<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateClassifyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        #书本基础信息
        Schema::create('book', function (Blueprint $table) {
            $table->increments('id');
            $table->string('cover')->default('')->comment('封面');
            $table->string('name')->default('')->comment('书名');
            $table->string('author')->default('')->comment('作者');
            $table->integer('word_ct')->default(0)->comment('单词数');
            $table->integer('study_ct')->default(0)->comment('学习人数');
            $table->integer('complete_ct')->default(0)->comment('完成人数');
            $table->integer('price')->default(0)->comment('价格');
            $table->timestamps();
        });

        #书本章节信息
        Schema::create('book_chapter', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('book_id')->default(0)->comment('书本ID')->index();
            $table->string('name')->unique()->comment('章节标题');
            $table->integer('sort')->default(0)->comment('排序');
            $table->longText('book_txt')->nullable()->comment('书本内容');
            $table->integer('word_ct')->default(0)->comment('单词数');
            $table->integer('is_free')->default(1)->comment('是否免费');
            $table->integer('price')->default(0)->comment('价格');
            $table->timestamps();
        });

        #分类
        Schema::create('classify', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique()->comment('名称');
            $table->integer('sort')->default(0)->comment('排序');
            $table->integer('is_show')->default(0)->comment('是否显示');
            $table->integer('book_ct')->default(0)->comment('书本数');
            $table->timestamps();
        });

        #分类书籍
        Schema::create('classify_book', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('classify_id')->default(0)->comment('分类ID');
            $table->integer('book_id')->default(0)->comment('书籍ID');
            $table->integer('sort')->default(0)->comment('排序');
            $table->timestamps();

            $table->unique(['classify_id', 'book_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('book');
        Schema::dropIfExists('classify');
        Schema::dropIfExists('classify_book');
    }
}
